package com.tongtian.mychess;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RJGameActivity extends Activity implements View.OnClickListener {

    private static final String TAG = "GameActivity";

    private static final int[][] images = new int[][]{
            {R.drawable.map_chess_02,R.drawable.map_chess_03,R.drawable.map_chess_04,R.drawable.map_chess_05,R.drawable.map_chess_06,R.drawable.map_chess_07,R.drawable.map_chess_08,R.drawable.map_chess_09,R.drawable.map_chess_10},
            {R.drawable.map_chess_11,R.drawable.map_chess_12,R.drawable.map_chess_13,R.drawable.map_chess_14,R.drawable.map_chess_15,R.drawable.map_chess_16,R.drawable.map_chess_17,R.drawable.map_chess_18,R.drawable.map_chess_19},
            {R.drawable.map_chess_20,R.drawable.map_chess_21,R.drawable.map_chess_22,R.drawable.map_chess_23,R.drawable.map_chess_24,R.drawable.map_chess_25,R.drawable.map_chess_26,R.drawable.map_chess_27,R.drawable.map_chess_28},
            {R.drawable.map_chess_29,R.drawable.map_chess_30,R.drawable.map_chess_31,R.drawable.map_chess_32,R.drawable.map_chess_33,R.drawable.map_chess_34,R.drawable.map_chess_35,R.drawable.map_chess_36,R.drawable.map_chess_37},
            {R.drawable.map_chess_38,R.drawable.map_chess_39,R.drawable.map_chess_40,R.drawable.map_chess_41,R.drawable.map_chess_42,R.drawable.map_chess_43,R.drawable.map_chess_44,R.drawable.map_chess_45,R.drawable.map_chess_46},
            {R.drawable.map_chess_47,R.drawable.map_chess_48,R.drawable.map_chess_49,R.drawable.map_chess_50,R.drawable.map_chess_51,R.drawable.map_chess_52,R.drawable.map_chess_53,R.drawable.map_chess_54,R.drawable.map_chess_55},
            {R.drawable.map_chess_56,R.drawable.map_chess_57,R.drawable.map_chess_58,R.drawable.map_chess_59,R.drawable.map_chess_60,R.drawable.map_chess_61,R.drawable.map_chess_62,R.drawable.map_chess_63,R.drawable.map_chess_64},
            {R.drawable.map_chess_65,R.drawable.map_chess_66,R.drawable.map_chess_67,R.drawable.map_chess_68,R.drawable.map_chess_69,R.drawable.map_chess_70,R.drawable.map_chess_71,R.drawable.map_chess_72,R.drawable.map_chess_73},
            {R.drawable.map_chess_74,R.drawable.map_chess_75,R.drawable.map_chess_76,R.drawable.map_chess_77,R.drawable.map_chess_78,R.drawable.map_chess_79,R.drawable.map_chess_80,R.drawable.map_chess_81,R.drawable.map_chess_82},
            {R.drawable.map_chess_83,R.drawable.map_chess_84,R.drawable.map_chess_85,R.drawable.map_chess_86,R.drawable.map_chess_87,R.drawable.map_chess_88,R.drawable.map_chess_89,R.drawable.map_chess_90,R.drawable.map_chess_91},

    };


    private RecyclerView recyclerView;
    private StaggeredGridLayoutManager staggeredGridLayoutManager;
    private List<ModelList> list;
    private MyAdapter myAdapter;
    private boolean isMove = false;//是否移动

    private ModelList modelListFirst;//上一次选中的棋子
    private int positionFirst;//上一次的下标

    private int piecesType=-1;//上一次选择的哪一方的棋子

    private  Map<Integer,ModelList[]> modelListMapBlue;//记录蓝方棋路

    private  Map<Integer,ModelList[]> modelListMapRed;//记录红方棋路

    private Button btnBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rjgame);

        initData();

        initView();

    }

    /**
     * 初始化数据
     */
    private void initData(){
        piecesType = -1;
        modelListMapBlue = new HashMap<>();
        modelListMapRed = new HashMap<>();
        list = new ArrayList<>();
        for (int i=0;i<9;i++){
            for (int j=0;j<10;j++){
                ModelList modelList = new ModelList();
                modelList.setMapImage(images[j][i]);
                modelList.setPieceType(0);
                modelList.setX(i);
                modelList.setY(j);
                list.add(modelList);
            }
        }

        //红方
        list.get(0).setPieces(Pieces.車);
        list.get(0).setPieceImage(R.drawable.che_r);
        list.get(0).setPieceType(2);

        list.get(10).setPieces(Pieces.馬);
        list.get(10).setPieceImage(R.drawable.ma_r);
        list.get(10).setPieceType(2);

        list.get(20).setPieces(Pieces.象);
        list.get(20).setPieceImage(R.drawable.xiang_r);
        list.get(20).setPieceType(2);

        list.get(30).setPieces(Pieces.士);
        list.get(30).setPieceImage(R.drawable.shi_r);
        list.get(30).setPieceType(2);

        list.get(40).setPieces(Pieces.帅);
        list.get(40).setPieceImage(R.drawable.shuai_r);
        list.get(40).setPieceType(2);

        list.get(50).setPieces(Pieces.士);
        list.get(50).setPieceImage(R.drawable.shi_r);
        list.get(50).setPieceType(2);

        list.get(60).setPieces(Pieces.象);
        list.get(60).setPieceImage(R.drawable.xiang_r);
        list.get(60).setPieceType(2);

        list.get(70).setPieces(Pieces.馬);
        list.get(70).setPieceImage(R.drawable.ma_r);
        list.get(70).setPieceType(2);

        list.get(80).setPieces(Pieces.車);
        list.get(80).setPieceImage(R.drawable.che_r);
        list.get(80).setPieceType(2);

        list.get(12).setPieces(Pieces.炮);
        list.get(12).setPieceImage(R.drawable.pao_r);
        list.get(12).setPieceType(2);

        list.get(72).setPieces(Pieces.炮);
        list.get(72).setPieceImage(R.drawable.pao_r);
        list.get(72).setPieceType(2);

        for (int i=0;i<5;i++){
            list.get(3+i*20).setPieces(Pieces.兵);
            list.get(3+i*20).setPieceImage(R.drawable.bing_r);
            list.get(3+i*20).setPieceType(2);
        }



        //蓝方
        list.get(9).setPieces(Pieces.車);
        list.get(9).setPieceImage(R.drawable.che_b);
        list.get(9).setPieceType(1);

        list.get(19).setPieces(Pieces.馬);
        list.get(19).setPieceImage(R.drawable.ma_b);
        list.get(19).setPieceType(1);

        list.get(29).setPieces(Pieces.象);
        list.get(29).setPieceImage(R.drawable.xiang_b);
        list.get(29).setPieceType(1);

        list.get(39).setPieces(Pieces.士);
        list.get(39).setPieceImage(R.drawable.shi_b);
        list.get(39).setPieceType(1);

        list.get(49).setPieces(Pieces.帅);
        list.get(49).setPieceImage(R.drawable.jiang_b);
        list.get(49).setPieceType(1);

        list.get(59).setPieces(Pieces.士);
        list.get(59).setPieceImage(R.drawable.shi_b);
        list.get(59).setPieceType(1);

        list.get(69).setPieces(Pieces.象);
        list.get(69).setPieceImage(R.drawable.xiang_b);
        list.get(69).setPieceType(1);

        list.get(79).setPieces(Pieces.馬);
        list.get(79).setPieceImage(R.drawable.ma_b);
        list.get(79).setPieceType(1);

        list.get(89).setPieces(Pieces.車);
        list.get(89).setPieceImage(R.drawable.che_b);
        list.get(89).setPieceType(1);

        list.get(17).setPieces(Pieces.炮);
        list.get(17).setPieceImage(R.drawable.pao_b);
        list.get(17).setPieceType(1);

        list.get(77).setPieces(Pieces.炮);
        list.get(77).setPieceImage(R.drawable.pao_b);
        list.get(77).setPieceType(1);

        for (int i=0;i<5;i++){
            list.get(6+i*20).setPieces(Pieces.兵);
            list.get(6+i*20).setPieceImage(R.drawable.zu_b);
            list.get(6+i*20).setPieceType(1);
        }

    }


    private void initView(){

        btnBack = (Button) findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        staggeredGridLayoutManager = new StaggeredGridLayoutManager(10,StaggeredGridLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        myAdapter = new MyAdapter(this,list);
        recyclerView.setAdapter(myAdapter);

        myAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                if (isMove){
                    ModelList modelList = list.get(position);
                    if (modelList.getPieceType()!=modelListFirst.getPieceType()){
                        if (modelListFirst.getPieces()==Pieces.車){

                            if (modelList.getX()==modelListFirst.getX()||modelList.getY()==modelListFirst.getY()){
                                if (cheLogic(modelList,position)){
                                    moveEnd(modelList,position);
                                }

                            }
                        }else if (modelListFirst.getPieces()==Pieces.馬){
                            int dx = Math.abs(modelList.getX()-modelListFirst.getX());
                            int dy = Math.abs(modelList.getY()-modelListFirst.getY());
                            if (( dx==2&&dy==1 ) ||( dx==1&&dy==2 )){
                                if (maLogic(modelList,position)){
                                    moveEnd(modelList,position);
                                }

                            }

                        } else if (modelListFirst.getPieces()==Pieces.象){
                            int dx = Math.abs(modelList.getX()-modelListFirst.getX());
                            int dy = Math.abs(modelList.getY()-modelListFirst.getY());
                            if (modelListFirst.getPieceType()==1){
                                if (dx==2&&dy==2 && modelList.getY()>=5){
                                    if (xiangLogic(modelList,position)){
                                        moveEnd(modelList,position);
                                    }

                                }
                            }else if (modelListFirst.getPieceType()==2){
                                if (dx==2&&dy==2 && modelList.getY()<=4){
                                    if (xiangLogic(modelList,position)){
                                        moveEnd(modelList,position);
                                    }

                                }
                            }


                        }else if (modelListFirst.getPieces()==Pieces.士){
                            int dx = Math.abs(modelList.getX()-modelListFirst.getX());
                            int dy = Math.abs(modelList.getY()-modelListFirst.getY());
                            if (modelListFirst.getPieceType()==1){
                                if (dx==1 && dy==1 && (modelList.getX()>2&&modelList.getX()<7) && modelList.getY()>6){
                                    moveEnd(modelList,position);
                                }
                            }else  if (modelListFirst.getPieceType()==2){
                                if (dx==1 && dy==1 && (modelList.getX()>2&&modelList.getX()<7) && modelList.getY()<3){
                                    moveEnd(modelList,position);
                                }
                            }


                        }else if (modelListFirst.getPieces()==Pieces.帅){

                            int dx = Math.abs(modelList.getX()-modelListFirst.getX());
                            int dy = Math.abs(modelList.getY()-modelListFirst.getY());
                            if (modelListFirst.getPieceType()==1){
                                if ((dx+dy==1) && (modelList.getX()>2&&modelList.getX()<7) && modelList.getY()>6){
                                    moveEnd(modelList,position);
                                }else {
                                    if (modelList.getPieces()==Pieces.帅){
                                        if (shuaiLogic(modelList,position)){
                                            moveEnd(modelList,position);
                                        }
                                    }
                                }
                            }else  if (modelListFirst.getPieceType()==2){
                                if ((dx+dy==1) && (modelList.getX()>2&&modelList.getX()<7) && modelList.getY()<3){
                                    moveEnd(modelList,position);
                                }else {
                                    if (modelList.getPieces()==Pieces.帅){
                                        if (shuaiLogic(modelList,position)){
                                            moveEnd(modelList,position);
                                        }
                                    }
                                }
                            }

                        }else if (modelListFirst.getPieces()==Pieces.炮){
                            if (modelList.getX()==modelListFirst.getX()||modelList.getY()==modelListFirst.getY()){
                                if (paoLogic(modelList,position)){
                                    moveEnd(modelList,position);
                                }
                            }

                        }else if (modelListFirst.getPieces()==Pieces.兵){
                            int dx = Math.abs(modelList.getX()-modelListFirst.getX());
                            int dy = Math.abs(modelList.getY()-modelListFirst.getY());
                            if (modelListFirst.getPieceType()==1){

                                if (modelListFirst.getY()>4){
                                    if (modelList.getX()==modelListFirst.getX() && modelList.getY()-modelListFirst.getY()==-1){
                                        moveEnd(modelList,position);
                                    }
                                }else {
                                    if (dx+dy==1 && modelList.getY()-modelListFirst.getY()<=0){
                                        moveEnd(modelList,position);
                                    }
                                }

                            }else  if (modelListFirst.getPieceType()==2){
                                if (modelListFirst.getY()<5){
                                    if (modelList.getX()==modelListFirst.getX() && modelList.getY()-modelListFirst.getY()==1){
                                        moveEnd(modelList,position);
                                    }
                                }else {
                                    if (dx+dy==1 && modelList.getY()-modelListFirst.getY()>=0){
                                        moveEnd(modelList,position);
                                    }
                                }
                            }

                        }
                    }else {
                        if (list.get(position).getPieceType()!=0){
                            //改变上一个的状态
                            list.get(positionFirst).setSel(false);
                            myAdapter.notifyItemChanged(positionFirst);
                            //记录选中棋子的信息并且改变状态
                            modelListFirst = list.get(position);
                            positionFirst = position;
                            isMove = true;
                            list.get(position).setSel(true);
                            myAdapter.notifyItemChanged(position);
                        }
                    }

                }else {

                    if (list.get(position).getPieceType()!=0){

                        if (list.get(position).getPieceType()==piecesType){
                            Toast.makeText(RJGameActivity.this,"等等对方执行",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //记录选中棋子的信息并且改变状态
                        modelListFirst = list.get(position);
                        positionFirst = position;
                        isMove = true;
                        list.get(position).setSel(true);
                        myAdapter.notifyItemChanged(position);
                    }
                }

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnBack:
                if (isMove){
                    isMove = false;
                    list.get(positionFirst).setSel(false);
                    myAdapter.notifyItemChanged(positionFirst);

                }else {
                    back();
                }

                break;
        }
    }

    /**
     * 移动结束
     * @param modelList 目的地的对象
     * @param position 目的地所对应的下标
     */
    private void moveEnd(ModelList modelList,int position){

        if (modelList.getPieces()==Pieces.帅){
            if (modelList.getPieceType()==1){

                showGameOverDialog("红方赢了！");
            }else if (modelList.getPieceType()==2) {

                showGameOverDialog("蓝方赢了！");
            }
        }


        if (modelListFirst.getPieceType()==1){//记录蓝方棋路
//            Log.e(TAG,"modelListFirst="+modelListFirst+",modelList="+modelList);
            ModelList model1 = getModelList(modelListFirst);
            ModelList model2 = getModelList(modelList);
            modelListMapBlue.put(modelListMapBlue.size()+1,new ModelList[]{model1,model2});
        }else {//记录红方棋路
            ModelList model1 = getModelList(modelListFirst);
            ModelList model2 = getModelList(modelList);
            modelListMapRed.put(modelListMapRed.size()+1,new ModelList[]{model1,model2});
        }

        piecesType = modelListFirst.getPieceType();
        modelList.setPieceImage(modelListFirst.getPieceImage());
        modelList.setPieceType(modelListFirst.getPieceType());
        modelList.setPieces(modelListFirst.getPieces());

        list.get(positionFirst).setPieceType(0);
        list.get(positionFirst).setPieceImage(0);
        list.get(positionFirst).setPieces(null);
        list.get(positionFirst).setSel(false);

        list.set(position,modelList);
        myAdapter.notifyItemChanged(positionFirst);
        myAdapter.notifyItemChanged(position);
        isMove = false;

    }

    /**
     * 车的逻辑
     * @param modelList 目的地的对象
     * @param position 目的地所对应的下标
     */
    private boolean cheLogic(ModelList modelList,int position){
        boolean canGo = true;
        if (modelList.getX()==modelListFirst.getX()){
            int dy = modelList.getY()-modelListFirst.getY();
            if (dy>0){
                for (int i=(modelListFirst.getY()+1);i<modelList.getY();i++){

                    if ( list.get(modelListFirst.getX()*10+i).getPieceType()!=0) {
                        return false;
                    }
                }
            }else {
                for (int i=(modelList.getY()+1);i<modelListFirst.getY();i++){

                    if ( list.get(modelListFirst.getX()*10+i).getPieceType()!=0) {
                        return false;
                    }
                }
            }

        }
        if (modelList.getY()==modelListFirst.getY()){
            int dx = modelList.getX()-modelListFirst.getX();
            if (dx>0){
                for (int i=(modelListFirst.getX()+1);i<modelList.getX();i++){

                    if (list.get(i*10+modelListFirst.getY()).getPieceType()!=0){
                        return false;
                    }
                }
            }else {
                for (int i=(modelList.getX()+1);i<modelListFirst.getX();i++){

                    if (list.get(i*10+modelListFirst.getY()).getPieceType()!=0){
                        return false;
                    }
                }
            }
        }

        return canGo;

    }

    /**
     * 馬的逻辑
     * @param modelList 目的地的对象
     * @param position 目的地所对应的下标
     */
    private boolean maLogic(ModelList modelList,int position){
        boolean canGo = true;
        if (modelList.getX()-modelListFirst.getX()==2){
            if (list.get((modelListFirst.getX()+1)*10+modelListFirst.getY()).getPieceType()!=0){
                canGo=false;
            }
        }

        if (modelList.getX()-modelListFirst.getX()==-2){
            if (list.get((modelListFirst.getX()-1)*10+modelListFirst.getY()).getPieceType()!=0){
                canGo=false;
            }
        }

        if (modelList.getY()-modelListFirst.getY()==2){
            if (list.get(modelListFirst.getX()*10+modelListFirst.getY()+1).getPieceType()!=0){
                canGo=false;
            }
        }

        if (modelList.getY()-modelListFirst.getY()==-2){
            if (list.get(modelListFirst.getX()*10+modelListFirst.getY()-1).getPieceType()!=0){
                canGo=false;
            }
        }

        return canGo;

    }

    /**
     * 象的逻辑
     * @param modelList 目的地的对象
     * @param position 目的地所对应的下标
     */
    private boolean xiangLogic(ModelList modelList,int position){
        boolean canGo = true;
        if (modelList.getX()-modelListFirst.getX()>0 && modelList.getY()-modelListFirst.getY()>0){
            int cx = modelListFirst.getX()+1;
            int cy = modelListFirst.getY()+1;
            if (list.get(cx*10+cy).getPieceType()!=0){
                canGo = false;
            }
        }

        if (modelList.getX()-modelListFirst.getX()>0 && modelList.getY()-modelListFirst.getY()<0){
            int cx = modelListFirst.getX()+1;
            int cy = modelListFirst.getY()-1;
            if (list.get(cx*10+cy).getPieceType()!=0){
                canGo = false;
            }
        }

        if (modelList.getX()-modelListFirst.getX()<0 && modelList.getY()-modelListFirst.getY()<0){
            int cx = modelListFirst.getX()-1;
            int cy = modelListFirst.getY()-1;
            if (list.get(cx*10+cy).getPieceType()!=0){
                canGo = false;
            }
        }

        if (modelList.getX()-modelListFirst.getX()<0 && modelList.getY()-modelListFirst.getY()>0){
            int cx = modelListFirst.getX()-1;
            int cy = modelListFirst.getY()+1;
            if (list.get(cx*10+cy).getPieceType()!=0){
                canGo = false;
            }
        }

        return canGo;

    }


    /**
     * 帅吃帅的的逻辑
     * @param modelList 目的地的对象
     * @param position 目的地所对应的下标
     */
    private boolean shuaiLogic(ModelList modelList,int position){
        boolean canGo = true;
        if (modelList.getX()==modelListFirst.getX()){
            int dy = modelList.getY()-modelListFirst.getY();
            if (dy>0){
                for (int i=(modelListFirst.getY()+1);i<modelList.getY();i++){

                    if ( list.get(modelListFirst.getX()*10+i).getPieceType()!=0) {
                       canGo = false;
                    }
                }
            }else {
                for (int i=(modelList.getY()+1);i<modelListFirst.getY();i++){

                    if ( list.get(modelListFirst.getX()*10+i).getPieceType()!=0) {
                        canGo = false;
                    }
                }
            }

        }else {
            canGo = false;
        }

        return canGo;

    }


    /**
     * 炮的逻辑
     * @param modelList 目的地的对象
     * @param position 目的地所对应的下标
     */
    private boolean paoLogic(ModelList modelList,int position){
        boolean canGo = true;
        if (modelList.getPieceType()==0){
            if (modelList.getX()==modelListFirst.getX()){
                int dy = modelList.getY()-modelListFirst.getY();
                if (dy>0){
                    for (int i=(modelListFirst.getY()+1);i<modelList.getY();i++){

                        if ( list.get(modelListFirst.getX()*10+i).getPieceType()!=0) {
                            return false;
                        }
                    }
                }else {
                    for (int i=(modelList.getY()+1);i<modelListFirst.getY();i++){

                        if ( list.get(modelListFirst.getX()*10+i).getPieceType()!=0) {
                            return false;
                        }
                    }
                }

            }
            if (modelList.getY()==modelListFirst.getY()){
                int dx = modelList.getX()-modelListFirst.getX();
                if (dx>0){
                    for (int i=(modelListFirst.getX()+1);i<modelList.getX();i++){

                        if (list.get(i*10+modelListFirst.getY()).getPieceType()!=0){
                            return false;
                        }
                    }
                }else {
                    for (int i=(modelList.getX()+1);i<modelListFirst.getX();i++){

                        if (list.get(i*10+modelListFirst.getY()).getPieceType()!=0){
                            return false;
                        }
                    }
                }
            }
        }else {
            int sum = 0;
            if (modelList.getX()==modelListFirst.getX()){
                int dy = modelList.getY()-modelListFirst.getY();
                if (dy>0){
                    for (int i=(modelListFirst.getY()+1);i<modelList.getY();i++){

                        if ( list.get(modelListFirst.getX()*10+i).getPieceType()!=0) {
                           sum++;
                        }
                    }
                }else {
                    for (int i=(modelList.getY()+1);i<modelListFirst.getY();i++){

                        if ( list.get(modelListFirst.getX()*10+i).getPieceType()!=0) {
                            sum++;
                        }
                    }
                }

            }
            if (modelList.getY()==modelListFirst.getY()){
                int dx = modelList.getX()-modelListFirst.getX();
                if (dx>0){
                    for (int i=(modelListFirst.getX()+1);i<modelList.getX();i++){

                        if (list.get(i*10+modelListFirst.getY()).getPieceType()!=0){
                            sum++;
                        }
                    }
                }else {
                    for (int i=(modelList.getX()+1);i<modelListFirst.getX();i++){

                        if (list.get(i*10+modelListFirst.getY()).getPieceType()!=0){
                            sum++;
                        }
                    }
                }
            }

            if (sum!=1){
                canGo = false;
            }
        }

        return canGo;

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode==KeyEvent.KEYCODE_BACK){
            if (isMove){
                isMove = false;
                list.get(positionFirst).setSel(false);
                myAdapter.notifyItemChanged(positionFirst);
                return true;
            }else {
                showDialog();
            }

        }

        return super.onKeyDown(keyCode, event);
    }

    private void showDialog(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示：");
        builder.setMessage("确定退出游戏吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();

    }

    private void showGameOverDialog(String msg){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示：");
        builder.setMessage(msg);
        builder.setPositiveButton("退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        });
        builder.setNegativeButton("再来一局", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

                    initData();
                    initView();

            }
        });
        builder.create().show();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    private void back(){
        int blue = modelListMapBlue.size();
        int red = modelListMapRed.size();
        Log.e(TAG, "back() returned: blue="+blue+",red="+red);
        if (blue>0||red>0){
            if (blue==red){
                //红棋回到上一步
                ModelList red1 = modelListMapRed.get(red)[0];
                ModelList red2 = modelListMapRed.get(red)[1];
                int posR1 = red1.getX()*10+red1.getY();
                int posR2 = red2.getX()*10+red2.getY();
                list.set(posR1,red1);
                list.set(posR2,red2);
                myAdapter.notifyItemChanged(posR1);
                myAdapter.notifyItemChanged(posR2);
                modelListMapRed.remove(red);

                //蓝棋回到上一步
                ModelList blue1 = modelListMapBlue.get(blue)[0];
                ModelList blue2 = modelListMapBlue.get(blue)[1];
                int posB1 = blue1.getX()*10+blue1.getY();
                int posB2 = blue2.getX()*10+blue2.getY();
                list.set(posB1,blue1);
                list.set(posB2,blue2);
                myAdapter.notifyItemChanged(posB1);
                myAdapter.notifyItemChanged(posB2);
                modelListMapBlue.remove(blue);

                //重制到蓝棋先走
                piecesType = 2;
                isMove = false;

            }else if (blue>red){

                //蓝棋回到上一步
                ModelList blue1 = modelListMapBlue.get(blue)[0];
                ModelList blue2 = modelListMapBlue.get(blue)[1];
//                Log.e(TAG,"blue1="+blue1.toString()+",blue2="+blue2.toString());
                int posB1 = blue1.getX()*10+blue1.getY();
                int posB2 = blue2.getX()*10+blue2.getY();
                list.set(posB1,blue1);
                list.set(posB2,blue2);
//                Log.e(TAG,"blue1="+blue1+",blue2="+blue2);
                myAdapter.notifyItemChanged(posB1);
                myAdapter.notifyItemChanged(posB2);
                modelListMapBlue.remove(blue);

                //重制到棋先走
                piecesType = 2;
                isMove = false;
            }
        }else {

        }

    }

    /**
     * 复制一个ModelList实体类
     * @param modelList
     * @return
     */
    private ModelList getModelList(ModelList modelList){
        ModelList model = new ModelList();
        model.setMapImage(modelList.getMapImage());
        model.setPieceImage(modelList.getPieceImage());
        model.setPieces(modelList.getPieces());
        model.setPieceType(modelList.getPieceType());
        model.setX(modelList.getX());
        model.setY(modelList.getY());
        return model;
    }

}
